<?php
class NewsAction extends Action {

    public function index(){

		$keys = explode(',',$seo_Arr);
		$SEO['title'] = '中国最新最全的建材求购,供应,招商,加盟信息平台-都市家建材网';
		$SEO['keywords'] = '建材,建材网,建材网站,建材新闻,建材供应,建材采购,建材招商,建材加盟,建材展会,建材企业,建材价格,建材销售,建材市场,建材产品,建材报价,建材品牌';
		$SEO['description'] = '专业提供建材新闻,主要内容有:建材价格,建材报价,建材品牌,建材招商加盟及建材代理信息,建材商品的求购,供应,招商与合作信息;为广大建材行业的个人与企业同行们，提供海量的建材信息与商机，努力打造成国内建材行业信息免费发布平台.';
		$this->assign('SEO',$SEO);// SEO相关
		
		$uid = $_SESSION[C('USER_AUTH_KEY')];
		
		$News   =   D('News');
		
		import("ORG.Util.Page");// 导入Pinyin类
		$Page = new Page();

		$admin['p'] = $currentpage;$_SESSION['cm_jumpurl'] = U('news/index',$admin);
		$p = $this->_get('p');
		$nowPage = isset($p)?$p:1;
		$where = "uid=$uid";
		$count      = $News->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,5);// 实例化分页类 传入总记录数
		$list = $News->where($where)->order('id desc')->page($nowPage,$Page->listRows)->select();
		//特殊处理URL
		//$Page->url = U('/uid/'.$c.'/__PAGE__');
		$Page->kylingood = 1;
		$Page->rollPage = 5;
		$pageshow = $Page->show();// 分页显示输出

		$this->assign('pages',$pageshow);// 赋值分页输出
		$this->assign('list',$list);// 赋值数据集


		$this->display(); // 输出模板

    }
	
	//发布新闻
	public function add(){

		if(IS_POST)
    	{
			$News   =   D('News');
			
			if($News->create()) {
				$data = $this->_post();
				$data['updatetime'] = $data['inputtime'] = time();
				$data['uid'] = $_SESSION[C('USER_AUTH_KEY')];
				$thumb = $data['thumb'];
				//移动缩略图片
				if ($thumb) {
					Load('extend');
					$filepath = str_replace('/tmp/','/news/',$thumb);
					$target = C('UPLOAD_DIR').$filepath;
					$source = C('UPLOAD_DIR').$thumb;
					if (!is_dir($target)) createDir(dirname($target));
					$r = rename($source,$target);
					if($r){
						$data['thumb'] = $filepath;
					}
				}

				
				$data['NewsData']['content'] = $data['content'];

				unset($data['content']);
				unset($data['btnSubmit']);
				
				//关联插入
				$id = $News->relation(true)->add($data);
				$d = array();
				
				if($id) {
					$d = array('id'=>$id);
					$this->ajaxReturn($d,'恭喜您，新闻发布成功！',1);
				}else{
					$this->ajaxReturn($d,'对不起，新闻发布失败！',0);
				}
			}else{

				//$this->error($Form->getError());
				$data['inputName'] = key($News->getError());
				$info = current($News->getError());
				$this->ajaxReturn($data,$info,0);
			}

		}else{
			
			
			$Category =   M('Category');
			$condition['parentid'] = 10;
			$CateArr = $Category->field('catname,catid')->where($condition)->select(); 
			$Categoryhtml = '<option value="">请选分类</option>';
			foreach($CateArr as $Cate) {
				$Categoryhtml.= '<option value="'.$Cate['catid'].'">'.$Cate['catname'].'</option>';
			}

			$this->assign('Categoryhtml',$Categoryhtml);
			$this->display();
		}
	}



	//修改新闻
	public function edit(){

		$id = $this->_param('id');
		$News   =   D('News');
		if(IS_POST){
			
			$data = $this->_post();
			

			$thumb = $data['thumb'];
			$thumb_old = $data['thumb_old'];
			//移动缩略图片
			if ($thumb && ($thumb_old != $thumb)) {
				Load('extend');
				$filepath = str_replace('/tmp/','/news/',$thumb);
				$target = C('UPLOAD_DIR').$filepath;
				$source = C('UPLOAD_DIR').$thumb;
				if (!is_dir($target)) createDir(dirname($target));
				$r = rename($source,$target);
				if($r){
					$data['thumb'] = $filepath;
				}
			}

			

			$data['NewsData']['content'] = $data['content'];
			unset($data['thumb_old']);
			unset($data['content']);
			unset($data['btnSubmit']);

			//print_r($data);
			//关联更新
			$result = $News->relation(true)->where("id=$id")->save($data);

			$d = array();
			if($result === false){
				$this->ajaxReturn($d,'对不起，新闻修改失败！',0);
			}else{
				$d = array('id'=>$id);
				
				$this->ajaxReturn($d,'恭喜您，新闻修改成功！',1);
				
			}
		
		}else{
			
			$data =    $News->relation(true)->find($id);
			//print_r($data);exit;
			if($data) {

				$this->data =   $data;// 模板变量赋值
				//取到大分类
				$Category =   M('Category');
				$condition['parentid'] = 10;
				$CateArr = $Category->field('catname,catid')->where($condition)->select(); 
				$Categoryhtml = '<option value="">请选分类</option>';
				foreach($CateArr as $Cate) {
					$selected = '';
					if($Cate['catid']==$data['catid']) $selected = 'selected="selected"';
					$Categoryhtml.= '<option value="'.$Cate['catid'].'" '.$selected.' >'.$Cate['catname'].'</option>';
				}

				$this->assign('Categoryhtml',$Categoryhtml);
				

			}else{
			
				echo 'error';
			}
		}
		$this->display(); 
	}

	//删除信息
	public function del(){
		if(IS_POST)
    	{
			$News   =   D('News');
			$id = $this->_param('id');
			$uid = $_SESSION[C('USER_AUTH_KEY')];;
			$result = $News->relation(true)->where("uid=$uid")->delete($id);
			if($result === false){
				$this->ajaxReturn($data,'对不起，删除出错。请稍后重试。',0);
			}else{
				$this->ajaxReturn($data,'恭喜您，删除成功！',1);
			}
		}
		exit;
	}
	


}